<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>React Router使用</title>
    <script src="https://cdn.bootcss.com/react/16.2.0/umd/react.development.js"></script>
    <script src="https://cdn.bootcss.com/react-dom/16.2.0/umd/react-dom.development.js"></script>

    <script src="https://cdn.bootcss.com/react-router/4.2.0/react-router.js"></script>
    <script src="https://cdn.bootcss.com/react-router-dom/4.2.2/react-router-dom.js"></script>

    <script src="https://cdn.bootcss.com/babel-standalone/6.26.0/babel.min.js"></script>

    <link href="https://cdn.bootcss.com/highlight.js/9.12.0/styles/vs2015.min.css" rel="stylesheet">
    <script src="https://cdn.bootcss.com/highlight.js/9.12.0/highlight.min.js"></script>

    <script>hljs.initHighlightingOnLoad();</script>
</head>
<body>
<div id="root"></div>

<p>代码</p>
<pre><code class="js">    ReactDOM.render(
        < h1 >欢迎使用React!< /h1>,
        document.getElementById('example')
    );
</code>
<script type="text/babel">
    const {
        BrowserRouter,
        Route,
        Link
    } = ReactRouterDOM;

    const Router = BrowserRouter;
    const BasicExample = () => (
        <Router>
            <div>
                <ul>
                    <li><Link to="/">首页</Link></li>
                    <li><Link to="/about">关于</Link></li>
                    <li><Link to="/topics">主题</Link></li>
                </ul>

                <hr/>

                <Route exact path="/" component={Home}/>
                <Route path="/about" component={About}/>
                <Route path="/topics" component={Topics}/>
            </div>
        </Router>
    )

    const Home = () => (
        <div>
            <h2>首页</h2>
        </div>
    )

    const About = () => (
        <div>
            <h2>关于</h2>
        </div>
    )
    const Topics = ({ match }) => (
        <div>
            <h2>主题</h2>
            <ul>
                <li>
                    <Link to={`${match.url}/rendering`}>
                        Rendering with React
                    </Link>
                </li>
                <li>
                    <Link to={`${match.url}/components`}>
                        Components
                    </Link>
                </li>
                <li>
                    <Link to={`${match.url}/props-v-state`}>
                        Props v. State
                    </Link>
                </li>
            </ul>

            <Route path={`${match.url}/:title`} component={Topic}/>
            <Route exact path={match.url} render={() => (
                <h3>请选择一个主题.</h3>
            )}/>
        </div>
    )

    const Topic = ({ match }) => (
        <div>
            <h3>{match.params.title}</h3>
        </div>
    )

    ReactDOM.render(
        <BasicExample />
        , document.getElementById('root')
    );
</script>

</body>
</html>